import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/lot_entity.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/ClearEditText.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';

import '../../bean/work_load_list_entity.dart';
import 'EndWorkNextOneController.dart';
import 'EndWorkNextTwoController.dart';

class EndWorkNextTwoPage extends StatelessWidget {
  final logic = Get.put(EndWorkNextTwoController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(USER),
        ),
        body: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [

                SubTitleBar("结束作业", '提交', () {
                  logic.trackOut();
                }),
                Container(
                  margin: EdgeInsets.all(8),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "添加工作量",
                    style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
                  ),
                ),
                SizedBox(
                  height: 4,
                ),
                GetBuilder<EndWorkNextTwoController>(
                  builder: (logic) => Align(
                    alignment: Alignment.center,
                    child: Row(
                      children: [
                        SizedBox(
                          width: 12,
                        ),
                        Container(
                          height: 36,
                          margin: EdgeInsets.only(right: 8),
                          child: ElevatedButton(
                            onPressed: () {
                              logic.addWork();
                            },
                            child: Text("添加"),
                          ),
                        ),
                        SizedBox(
                          width: 4,
                        ),
                      ],
                    ),
                  ),
                ),
                SizedBox(
                  height: 4,
                ),
                GetBuilder<EndWorkNextTwoController>(
                    builder: (logic) => Table(
                          defaultVerticalAlignment:
                              TableCellVerticalAlignment.middle,
                          columnWidths: {
                            //列宽
                            0: FlexColumnWidth(2.0),
                            1: FlexColumnWidth(1.0),
                            2: FlexColumnWidth(2.0),
                          },
                          border: TableBorder.all(
                            color: Colors.grey,
                            width: 1.0,
                            style: BorderStyle.solid,
                          ),
                          children: getDefectList(context),
                        ), id: logic.listID,
                ),
                TitleEditText(
                  "机台数量:",
                  "请输入机台数量",
                      (value) {
                  },
                  controller: logic.controller,
                ),
              ],
            ),
          ),
        ));
  }

  List<TableRow> getDefectList(BuildContext context) {
    List<TableRow> tempView = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                '批次号',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '工号',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < logic.defectList.length; i++) {
      WorkLoadListEntity element = logic.defectList[i];
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.lotId ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: ClearEditText(
            "",
                (value) {
              element.userName = value;
            },
            isScan: false,isShowIcon: false,
            controller: TextEditingController(text: element.userName ?? ''),
          ),
        ),
        Align(
          alignment: Alignment.topCenter,
          child: Container(
            padding: EdgeInsets.only(bottom: 4),
            child: ClearEditText(
              "",
              (value) {
                if (value.isEmpty) {
                  value = "0.0";
                }
                element.mainQty = value.toDouble();
              },
              type: TextInputType.number,
              isScan: false,isShowIcon: false,
              controller: TextEditingController(text: element.mainQty == null ? '0.0' : element.mainQty.toString()),
            ),
          ),
        )
      ]));
    }
    return tempView;
  }
}
